/**
 * Copyright 2009 Sergio Bossa
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.actorom.local;

import com.googlecode.actorom.Actor;
import com.googlecode.actorom.Address;
import com.googlecode.actorom.FailoverPolicy;
import com.googlecode.actorom.ThreadingPolicy;
import com.googlecode.actorom.impl.core.AbstractTopology;
import com.googlecode.actorom.local.support.SymbolicHostBinder;
import com.googlecode.actorom.support.FailoverPolicies;
import com.googlecode.actorom.support.ThreadingPolicies;
import com.googlecode.actorom.support.TopologyException;
import net.jcip.annotations.ThreadSafe;

/**
 * Local {@link com.googlecode.actorom.Topology} implementation to use for in-memory, in-vm,
 * actors.<br>
 * A LocalTopology instance is able to:
 * <ul>
 * <li>Locally spawn actors.</li>
 * <li>Access its own local actors by using their {@link Address}, but not actors spawned
 * by other LocalTopology instances.</li>
 * </ul>
 * Each LocalTopology is bound to a symbolic host name defined at construction time, which is used to identify the
 * topology itself and create its address space: you cannot bind the same name to more than one active topology.
 *
 * @author Sergio Bossa
 */
@ThreadSafe
public class LocalTopology extends AbstractTopology {

    private final String symbolicHost;

    /**
     * Create a local topology using the following defaults:
     * <ul>
     * <li>{@link com.googlecode.actorom.support.ThreadingPolicies#newOSThreadingPolicy(int )}, using a thread pool
     * with a number of fixed threads equal to the available processors.</li>
     * <li>{@link com.googlecode.actorom.support.FailoverPolicies#newNoFailoverPolicy()}.</li>
     * </ul>
     *
     * @param symbolicHost The symbolic host name this local topology must be bound to; the name must not be bound to any other active topology.
     */
    public LocalTopology(String symbolicHost) {
        this(symbolicHost, ThreadingPolicies.newOSThreadingPolicy(Runtime.getRuntime().availableProcessors()), FailoverPolicies.newNoFailoverPolicy());
    }

    /**
     * Create a local topology using the provided {@link com.googlecode.actorom.ThreadingPolicy},
     * and the {@link com.googlecode.actorom.support.FailoverPolicies#newNoFailoverPolicy()} default.
     * 
     * @param symbolicHost The symbolic host name this local topology must be bound to; the name must not be bound to any other active topology.
     * @param threadingPolicy
     */
    public LocalTopology(String symbolicHost, ThreadingPolicy threadingPolicy) {
        this(symbolicHost, threadingPolicy, FailoverPolicies.newNoFailoverPolicy());
    }

    /**
     * Create a local topology using the provided {@link com.googlecode.actorom.FailoverPolicy},
     * and the {@link com.googlecode.actorom.support.ThreadingPolicies#newGreenThreadingPolicy()} default.
     *
     * @param symbolicHost The symbolic host name this local topology must be bound to; the name must not be bound to any other active topology.
     * @param failoverPolicy
     */
    public LocalTopology(String symbolicHost, FailoverPolicy failoverPolicy) {
        this(symbolicHost, ThreadingPolicies.newGreenThreadingPolicy(), failoverPolicy);
    }

    /**
     * Create a local topology using the provided {@link com.googlecode.actorom.ThreadingPolicy} and
     * {@link com.googlecode.actorom.FailoverPolicy}.
     *
     * @param symbolicHost The symbolic host name this local topology must be bound to; the name must not be bound to any other active topology.
     * @param threadingPolicy
     * @param failoverPolicy 
     */
    public LocalTopology(String symbolicHost, ThreadingPolicy threadingPolicy, FailoverPolicy failoverPolicy) {
        super(threadingPolicy, failoverPolicy);
        this.symbolicHost = symbolicHost;
        SymbolicHostBinder.getInstance().bind(symbolicHost);
    }

    public final synchronized Address spawnActor(String id, Object handler) {
        Address address = Address.newLocalAddress(symbolicHost, id);
        boolean spawned = doSpawnActor(address, handler);
        if (spawned) {
            return address;
        } else {
            return null;
        }
    }

    public final synchronized Actor spawnAndGetActor(String id, Object handler) {
        Address address = Address.newLocalAddress(symbolicHost, id);
        boolean spawned = doSpawnActor(address, handler);
        if (spawned) {
            return doGetActor(address);
        } else {
            return null;
        }
    }

    public final synchronized Actor getActor(Address address) {
        if (address.isLocal()) {
            return doGetActor(address);
        } else {
            throw new TopologyException("Not a local address: " + address);
        }
    }

    public final synchronized void shutdown() {
        SymbolicHostBinder.getInstance().unbind(symbolicHost);
        doShutdown();
    }
}
